package com.mapler.ngfx.ui;

import com.mapler.utility.HttpHelper;
import com.mapler.utility.IConstant;
import java.awt.Color;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Random;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;

/**
 *
 * @author none
 */
public class NGFXScriptUI extends javax.swing.JPanel {

    private String username;
    private String password;
    private String country;
    private static Logger log = Logger.getLogger(NGFXScriptUI.class);

    /**
     * Creates new form AddScriptUI
     */
    public NGFXScriptUI() {
        initComponents();
    }

    public NGFXScriptUI(String username, String password, String country) {
        this.username = username;
        this.password = password;
        this.country = country;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        emailDomain = new javax.swing.JTextField();
        message = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        fwEmail = new javax.swing.JTextField();
        fwEmailPass = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        noOfAccount = new javax.swing.JTextField();
        add = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        mailAccount = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        useEmail = new javax.swing.JCheckBox();

        message.setText("message");

        jLabel2.setText("Email domain");

        jLabel3.setText("Fw Eamil");

        jLabel4.setText("Fw email pass");

        add.setText("Add");
        add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addActionPerformed(evt);
            }
        });

        jLabel6.setText("No of accounts");

        jLabel7.setText("Email Account");

        useEmail.setText("Use email account");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(89, 89, 89)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(add, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel2)
                                            .addComponent(jLabel3)
                                            .addComponent(jLabel4)
                                            .addComponent(jLabel7))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(noOfAccount, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)
                                                .addComponent(fwEmailPass)
                                                .addComponent(fwEmail)
                                                .addComponent(emailDomain))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(mailAccount, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(useEmail)))))
                                .addGap(24, 24, 24))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(message, javax.swing.GroupLayout.PREFERRED_SIZE, 570, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(message, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(emailDomain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(mailAccount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(useEmail))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fwEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fwEmailPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(noOfAccount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 135, Short.MAX_VALUE)
                .addComponent(add)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addActionPerformed
        try {
            if (fwEmail.getText().isEmpty() || fwEmailPass.getText().isEmpty()
                    || noOfAccount.getText().isEmpty()) {
                this.message.setText("Forward email, forward email pass, link, proxy, No of Account cannot be empty");
                return;
            }

            if (useEmail.isSelected()) {
                if (mailAccount.getText().isEmpty() && !mailAccount.getText().contains("@")) {
                    this.message.setText("Email account field cannot be empty or not containing @ sign");
                    return;
                }
            } else {
                if (emailDomain.getText().isEmpty()) {
                    this.message.setText("Email domain cannot be empty");
                    return;
                }
            }

            String mailparts[] = mailAccount.getText().split("@");
            int noofa = Integer.parseInt(noOfAccount.getText());
            String[] charset = {"a", "b","1", "c", "d", "e","2", "f", "g", "h","3", "i", "j", "k", "4","l", "m", "n", "o","5", "p", "q", "r", "6","s", "t","7", "u", "v","8", "w", "x", "y","9", "z"};
            for (int i = 1; i <= noofa; i++) {
                // Email creattion
                String email = "";
                for (int j = 1; j <= 7; j++) {
                    email += charset[this.getRandom().nextInt(charset.length - 1)];
                }
                if (useEmail.isSelected()) {
                    email = mailparts[0] + "+" + email + "@" + mailparts[1];
                } else {
                    email += emailDomain.getText();
                }

                StringBuilder request = new StringBuilder();
                request.append("<Request>");
                request.append("<username>").append(this.username).append("</username>");
                request.append("<password>").append(this.password).append("</password>");
                request.append("<email>").append(email).append("</email>");
                request.append("<email_pass>").append("").append("</email_pass>");
                request.append("<fw_email>").append(this.fwEmail.getText()).append("</fw_email>");
                request.append("<fw_email_pass>").append(this.fwEmailPass.getText()).append("</fw_email_pass>");
                request.append("<ngfx_username>").append("").append("</ngfx_username>");
                request.append("<ngfx_password>").append("").append("</ngfx_password>");
                request.append("<verify_code1>").append("").append("</verify_code1>");
                request.append("<verify_code2>").append("").append("</verify_code2>");
                request.append("<date_of_birth>").append("").append("</date_of_birth>");
                request.append("</Request>");

                String req = URLEncoder.encode(request.toString(), "UTF-8");
                String uri = "http://" + IConstant.HOSTNAME + "/index.php?r=ngfxaccount/addAccount";
                String param = "request=" + req;
                String response = HttpHelper.post(new URL(uri), param);

                Document document = DocumentHelper.parseText(response);
                String errorCode = document.valueOf("Response/errorcode");
                String msg = document.valueOf("Response/message");
                if (errorCode.equalsIgnoreCase("000")) {
                    this.message.setBackground(Color.red);
                } else {
                    this.message.setBackground(Color.black);
                }
                this.message.setText(msg);
            }

        } catch (Throwable ex) {
            message.setText("Error on cl account creation. causes " + ex);
            log.debug("Error on cl account creation. causes. " + ex);
        }

    }//GEN-LAST:event_addActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton add;
    private javax.swing.JTextField emailDomain;
    private javax.swing.JTextField fwEmail;
    private javax.swing.JTextField fwEmailPass;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField mailAccount;
    private javax.swing.JLabel message;
    private javax.swing.JTextField noOfAccount;
    private javax.swing.JCheckBox useEmail;
    // End of variables declaration//GEN-END:variables
     public void clearFields(){
        try{
            emailDomain.setText("");
            emailDomain.setText("");
            fwEmailPass.setText("");
            mailAccount.setText("");
            noOfAccount.setText("");
            useEmail.setSelected(false);
            message.setText("");
            fwEmail.setText("");
        }catch( Throwable ex ) {
            ex.printStackTrace();
            log.debug("clearFields:: Error causes "+ex);
        }
    }
     
    private Random random;

    public Random getRandom() {
        if (random == null) {
            random = new Random();
        }
        return random;
    }

    public void setRandom(Random random) {
        this.random = random;
    }
}
